﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyTruongHoc_App
{
    public partial class QLMonHoc : Form
    {
        public QLMonHoc()
        {
            InitializeComponent();
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_QL_MON_HOC);
            // user co quyen view
            /*if (!((pow & PConst.POW_VIEW) > 0))
            {
                MessageBox.Show(MSG.MSG_KHONG_DU_QUYEN);
                this.Close();
            }*/
            // user co quyen them
            tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);

            // user co quyen edit
            tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);
            
            // user co delete
            dg_DanhSach.AllowUserToDeleteRows =
                tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void refreshData()
        {
            tbl_MonHocTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();
            tbl_MonHocTableAdapter.ClearBeforeFill = true;
            this.tbl_MonHocTableAdapter.FillByFlag(this.qLTH.tbl_MonHoc, PConst.FLAG_NONE);
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            AddEditMonHoc frm_AddMH = new AddEditMonHoc();
            frm_AddMH.MdiParent = this.MdiParent;
            frm_AddMH.Show();
            frm_AddMH.Disposed += new EventHandler(frm_AddMH_Disposed);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            int i = int.Parse(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            AddEditMonHoc frm_EditMH = new AddEditMonHoc();
            frm_EditMH.MdiParent = this.MdiParent;
            frm_EditMH.Show();
            frm_EditMH.setEditInfo(i);
            frm_EditMH.Disposed += new EventHandler(frm_AddMH_Disposed);
        }

        private void frm_AddMH_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void fillSTT()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_STT.Name].Value = (i + 1).ToString();
            }
        }

        private void dg_DanhSach_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            fillSTT();
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            }
            else
                e.Cancel = true;
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            }
        }

        private void deleteByID(String _id)
        {
        
            try
            {
                DataRow[] tmp = qLTH.tbl_MonHoc.Select(qLTH.tbl_MonHoc.idColumn.ColumnName + "=" + _id);
                if (tmp.Length > 0)
                {
                    tmp[0][qLTH.tbl_MonHoc.flagColumn] = PConst.FLAG_DELETE;
                    tbl_MonHocTableAdapter.Update(qLTH.tbl_MonHoc);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void QLLoaiDiem_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 15;
            dg_DanhSach.Height = stts_Bottom.Top - dg_DanhSach.Top - 1;
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            bt_Sua_Click(tsb_Edit, new EventArgs());
        }

        private void QLMonHoc_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }
    }
}
